<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/dao/CalendarDao.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/dao/KidDao.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/dao/UserDao.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/utils/CalendarUtils.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Kid.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/web/Session.php');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CalendarDao
 *
 * @author virtual
 */
class CalendarService {
    private $calendarDao;
    private $kidDao;
    private $userDao;

    public function __construct(){
        $this->calendarDao = new CalendarDao();
        $this->kidDao = new KidDao();
        $this->kidDao = new UserDao();
    }
    
    
    /**
     * <calendar kidId="1" month="01" year="2012">
     *      <calendarItem id="20111230" startTime="" endTime="" registered="true"/>  --> always monday
     *      <calendarItem id="20111231" startTime="" endTime="" registered="true"/>
     * </calendar>
     * 
     * STATUS::DONE NOT TESTED
     */
    public function readKidCalendarInfoXml($kidId, $year, $month) {
        if(!$this->accessAllowed($kidId)){
            return null;
        }
        $kid = new Kid();
        $kid->setId($kidId);
        $calendarArray = $this->calendarDao->readCalendarByKidMonth($kid, $year, $month);
        $monthLabel = CalendarUtils::getMonthLabel($month);
        
        $result = "<calendarMonth kidId=\"$kidId\" month=\"$month\" year=\"$year\" monthLabel=\"$monthLabel\" defaultStart=\"10:00\" defaultEnd=\"18:00\">";
        foreach($calendarArray as $calendar){
            $result .= $calendar->toXml();
        }
        $result .= "</calendarMonth>";
        return $result;
        
    }
    
    /**
     *
     * @param type $kidId
     * @param type $days
     * @return string 
     * 
     * STATUS: DONE NOT TESTED
     */
     public function readNextCalendarDaysForKidXml($kidId, $days) {
        $kid = new Kid();
        $kid->setId($kidId);
        $calendarArray = $this->calendarDao->getNextCalendarDaysForKid($kid, $days);
        
        $result = "<calendarMonth kidId=\"$kidId\" days=\"$days\">";
        foreach($calendarArray as $calendar){
            $result .= $calendar->toXml();
        }
        
        $result .= "</calendarMonth>";
        return $result;
     }
    
    
     /**
      *
      * @param type $calendar 
      * STATUS: DONE TO TEST
      */
    public function saveCalendarTimeForKid($calendar, $type){
        return $this->calendarDao->createCalendar($calendar, $type);
    }

    
        
    //return xml with all kids comming on that day
    //<kids date="YYYYMMDD">
    //  <kid name firstname from to>
    //</kids>
    
    public function readKidsCommingTodayXml(){
        $today = date('Ymd');	
        
        $result = "<kids date=\"$today\">";
        $result .= $this->calendarDao->readCalendarKidsForDate($today)->toXml();
        
        $result .= "</kids>";
        return $result;
    }
    
    public function readKidsCommingToday(){
        $today = date('Ymd');	
              
        return $this->calendarDao->readCalendarKidsForDate($today);
    }
    
   
    
    //return a calendar displaying how many kids are comming every day
    //<calendar>
    //  <day id="yyyymmdd>
    //      <kids count="1">
    //          <kid name from to>
    //      </kids>
    //  </day>
    //</calendar>
    public function readKidsCommingThisMonthXml(){
        $month = date('m');	
        $year = date('Y');	
        
        return $this->calendarDao->readCalendarKidsForMonth($year, $month).toXml();
        
    }
    
    public function readKidsCommingThisMonth() {
        $month = date('m');	
        $year = date('Y');	
        
        return $this->calendarDao->readCalendarKidsForMonth($year, $month);
        
    }
    
    public function readKidsCommingThisWeek() {
        $month = date('m');	
        $year = date('Y');	
        $day = date('d');	
        
        return $this->calendarDao->readCalendarKidsForWeek($year, $month, $day);
        
    }
    
    public function readKidsCommingInWeekXml($datekey) {
         $ydmPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})$/";
        
        preg_match($ydmPattern, $datekey, $matches);
		if(sizeof($matches) == 4){
            $year = $matches[1]; 
            $month = $matches[2];
            $day = $matches[3];
        }
        
        $prev = date("Ymd", mktime(0,0,0,$month, $day-7, $year));
        $next = date("Ymd", mktime(0,0,0,$month, $day+7, $year));
        $monthbeginweek = date("m", mktime(0,0,0,$month, $day-7, $year));
        $monthlabel = CalendarUtils::getMonthLabel($monthbeginweek);
        
        $weekIndex = date("N", mktime(0, 0, 0, $month, $day, $year));
        $thisBeginWeek = date("d", mktime(0,0,0, $month, $day-$weekIndex+1, $year));
   
        
        $result = "<kidsInWeek next=\"$next\" prev=\"$prev\" monthlabel=\"$monthlabel\" day=\"$thisBeginWeek\" >";
        
        $kids = array();
        //read calendar->kids
        $calendarInfo = $this->calendarDao->readCalendarKidsForWeek($year, $month, $day);
   
        //convert to kids->calendar
        //fill in array where kids are comming
        foreach($calendarInfo as $calendarKids){
            foreach($calendarKids->getKids() as $kid){
                $kids[$kid->getName() . " " . $kid->getFirstName()][$calendarKids->getDate()] = "Y";
            } 
        }
    
        //fill in missing elements
        foreach(array_keys($kids) as $key){
            $result .= "<kid name=\"$key\">";
             foreach($calendarInfo as $calendarKids){
                 if(isset($kids[$key][$calendarKids->getDate()])){
                    $result .= "<date day=\"" .substr($calendarKids->getDate(),6) ."\" in=\"Y\"/>";
                        
                }else {
                    $result .= "<date day=\"" .substr($calendarKids->getDate(),6) ."\" in=\"N\"/>";
                }
             }
             $result .= "</kid>";
        }
        
        $result .= "</kidsInWeek>";
    
        return $result;
        
        
    }
    

    //get current month calendar for kid for a given month and year
    public function getCalendarForKid($kid, $year, $month){
        return $this->calendarDao->readCalendarForKid($kid, $year, $month);
    }
    
    public function accessAllowed($kidId) {
        
        $username = Session::getUser();
        //not logged in return false
        if($username == null)
            return false;
        
        //admin has all access
        if(Session::getRole() == "ADMIN")
            return true;
        
        if(Session::getRole() == "KIDEDIT" ) {
            return $this->userDao->userHasAccessToKid($username, $kidId);
        }
    }
    
  
}

?>
